package com.kate.mall.order.price;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.kate.mall.order.dto.OrderItemDTO;
import com.kate.mall.order.dto.PromotionActivityDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * @Description:  单品促销活动的价格计算组件
 * @Auther: 新梦想*陈超
 * @Date: 2021/10/30 19:34
 */
@Component
@Slf4j
public class DirectDiscountPromotionActivityCalcualtor implements PromotionActivityCalculator {

	/**
	 * 计算促销活动的减免金额
	 */
	@Override
	public PromotionActivityResult calculate(OrderItemDTO item,
											 PromotionActivityDTO promotionActivity) throws Exception {
		BigDecimal totalAmount =  item.getPrice().multiply(new BigDecimal(item.getNumber()));

		JSONObject rule = JSONUtil.parseObj (promotionActivity.getRule());

		BigDecimal discountRate = rule.get("discountRate", BigDecimal.class);

		BigDecimal discountAmountRate = BigDecimal.valueOf(1.0).subtract(discountRate);
		
		BigDecimal discountAmount = totalAmount.multiply(discountAmountRate);
		log.info("====单品促销活动价格 计算器，计算活动减免价为：{}====",discountAmount);
		return new PromotionActivityResult(discountAmount);      
	}
	
}
